<?php

namespace api\modules\v1\controllers;

use api\controllers\OnAuthController;
use api\modules\v1\forms\member\MemberForm;
use common\helpers\CommonHelper;
use common\models\member\Member;
use services\member\MemberService;
use Yii;

/**
 * Class MemberController
 * @package api\modules\v1\controllers
 */
class MemberController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/member/list  用户列表
     * @apiName list
     * @apiGroup Member
     * @apiUse CommonParam
     *
     * @apiParam {String} keyword           关键词
     * @apiParam {String} project_id        项目【多个用','分割】
     * @apiParam {String} role_id           角色【多个用','分割】
     * @apiParam {Number} status            状态
     * @apiParam {String} page              页码
     * @apiParam {String} pageSize          每页展示数
     *
     * @apiSuccess {Object}  data                           返回数据
     * @apiSuccess {Number}  data.page                      页码
     * @apiSuccess {Number}  data.pageSize                  每页展示数
     * @apiSuccess {Number}  data.total                     总数
     * @apiSuccess {Array}   data.list                      列表数据
     * @apiSuccess {Number}  data.list.id                   用户ID
     * @apiSuccess {String}  data.list.username             姓名
     * @apiSuccess {String}  data.list.mobile               手机号
     * @apiSuccess {Array}   data.list.project_name_arr     项目
     * @apiSuccess {Array}   data.list.department_name_arr  部门
     * @apiSuccess {String}  data.list.job_name             岗位
     * @apiSuccess {Array}   data.list.role_name_arr        角色
     * @apiSuccess {String}  data.list.status_str           状态
     * @apiSuccess {String}  data.list.last_time_str        登陆时间
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = CommonHelper::explode(',', $params['project_id'] ?? '');
        $params['role_id'] = CommonHelper::explode(',', $params['role_id'] ?? '');

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new MemberService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        // 筛选项
//        $data['condition'] = $service->getListCondition();
        return $data;
    }

    /**
     * @api {get} /v1/member/list-condition  列表筛选条件
     * @apiName list-condition
     * @apiGroup Member
     * @apiUse CommonParam
     *
     * @apiSuccess {Object}  data                   返回数据
     * @apiSuccess {Array}   data.project           项目【{value:'0', label: '项目'}】
     * @apiSuccess {Array}   data.role              角色【{value:'0', label: '普通'}】
     * @apiSuccess {Array}   data.status            状态【{value:'0', label: '普通'}】
     */
    public function actionListCondition()
    {
        $params = Yii::$app->request->get();

        $service = new MemberService();
        return $service->getSelectCondition();
    }

    /**
     * @api {get} /v1/member/info 用户详情
     * @apiName info
     * @apiGroup Member
     * @apiUse CommonParam
     *
     * @apiParam {Number} id                                用户ID
     *
     * @apiSuccess {Object} data                            返回内容
     * @apiSuccess {Number} data.id                         ID
     * @apiSuccess {String} data.username                   用户名
     * @apiSuccess {String} data.mobile                     手机号
     * @apiSuccess {String} data.status                     状态
     * @apiSuccess {Array}  data.project_id                 项目IDs
     * @apiSuccess {Array}  data.department_id              部门IDs
     * @apiSuccess {Number} data.job_id                     岗位ID
     * @apiSuccess {Array}  data.role_id                    角色IDs
     * @apiSuccess {Object} data.condition                  筛选条件
     * @apiSuccess {Array}  data.condition.project          项目【{value:'0', label: '普通'},{value:'2', label: '管理员'}】
     * @apiSuccess {Array}  data.condition.department       部门【{value:'0', label: '普通'},{value:'2', label: '管理员'}】
     * @apiSuccess {Array}  data.condition.job              岗位【{value:'0', label: '普通'},{value:'2', label: '管理员'}】
     * @apiSuccess {Array}  data.condition.role             角色【{value:'0', label: '普通'},{value:'2', label: '管理员'}】
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $params['id'] = $params['id'] ?? 0;

        $service = new MemberService();
        return $service->getInfo((int)$params['id']);
    }

    /**
     * @api {get} /v1/member/change-dep-info 修改部门获取筛选条件
     * @apiName change-dep-info
     * @apiGroup Member
     * @apiUse CommonParam
     *
     * @apiParam {Array} department_id                      部门IDs
     * @apiParam {Array} role_id                            选中角色IDs【当前选中角色IDs】
     *
     * @apiSuccess {Object} data                            返回内容
     * @apiSuccess {Number} data.job_id                     岗位ID
     * @apiSuccess {Array}  data.role_id                    角色IDs【返回计算结果】
     * @apiSuccess {Object} data.condition                  筛选条件
     * @apiSuccess {Array}  data.condition.role             角色【{value:'0', label: '普通'},{value:'2', label: '管理员'}】
     */
    public function actionChangeDepInfo()
    {
        $params = Yii::$app->request->get();
        $params['department_id'] = $params['department_id'] ?? [];
        $params['role_id'] = $params['role_id'] ?? [];

        $service = new MemberService();
        return $service->getChangeMemberDepartmentIds((array)$params['department_id'], (array)$params['role_id']);
    }

    /**
     * @api {post} /v1/member/create 新增用户
     * @apiName create
     * @apiGroup Member
     * @apiUse CommonParam
     *
     * @apiBody {String} username          用户名
     * @apiBody {String} mobile            手机号
     * @apiBody {String} password          密码【不填，则默认密码jgwl123546】
     * @apiBody {Array}  project_ids       项目IDs
     * @apiBody {Array}  department_ids    部门IDs
     * @apiBody {Array}  role_ids          角色IDs
     * @apiBody {Number} job_id            岗位ID
     * @apiBody {Number} status            状态
     *
     * @apiSuccess {Object} data            返回内容
     * @apiSuccess {Number} data.id         用户ID
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();
        $params['password'] = $params['password'] ?? '';
        if (empty($params['password'])) {
            $params['password'] = Member::DEFAULT_PASSWORD;
        }

        $model = new MemberForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new MemberService();
        return $service->create($model);
    }

    /**
     * @api {post} /v1/member/update 编辑用户
     * @apiName update
     * @apiGroup Member
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                用户ID
     * @apiBody {String} username          用户名
     * @apiBody {String} mobile            手机号
     * @apiBody {String} password          密码【不填，则不修改】
     * @apiBody {Array}  project_ids       项目IDs
     * @apiBody {Array}  department_ids    部门IDs
     * @apiBody {Array}  role_ids          角色IDs
     * @apiBody {Number} job_id            岗位ID
     * @apiBody {Number} status            状态
     *
     * @apiSuccess {Object} data            返回内容
     * @apiSuccess {Number} data.id         用户ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();

        $model = new MemberForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new MemberService();
        return $service->update($model);
    }

    /**
     * @api {post} /v1/member/delete 删除用户
     * @apiName delete
     * @apiGroup Member
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                用户ID
     *
     * @apiSuccess {Object} data            返回内容
     * @apiSuccess {Number} data.id         用户ID
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();

        $model = new MemberForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new MemberService();
        return $service->delete($model);
    }

    /**
     * @api {post} /v1/member/update-password 修改密码【自己】
     * @apiName update-password
     * @apiGroup Member
     * @apiUse CommonParam
     *
     * @apiBody {Number} password              原密码
     * @apiBody {Number} new_password          新密码
     * @apiBody {Number} confirm_password      确认密码
     *
     * @apiSuccess {Object} data                返回内容
     * @apiSuccess {Number} data.id             用户ID
     */
    public function actionUpdatePassword()
    {
        $params = Yii::$app->request->post();
        $params['id'] = Yii::$app->user->identity->member_id;

        $model = new MemberForm();
        $model->setScenario('update_password');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new MemberService();
        return $service->updatePassword($model);
    }
}
